//
//  HNSH_PurseViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/26.
//

#import "HNSH_PurseViewController.h"

//view
#import "HNSH_PurseTopView.h"
#import "CGXDatePickerView.h"

#import "HNSH_PurseCell.h"

#import "WSDatePickerView.h"

@interface HNSH_PurseViewController ()

/**  */
@property (nonatomic, strong)HNSH_PurseTopView * headerView;
/**  */
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
/**  */
@property (nonatomic, strong)NSMutableArray * dataArray;
/**  */
@property (nonatomic, assign)NSInteger page;
/**  */
@property (nonatomic, copy)NSString * currentDate;
/**  */
@property (nonatomic, copy)NSString * year;
/**  */
@property (nonatomic, copy)NSString * month;
/**  */
@property (nonatomic, copy)NSString * day;

/* capital_list */
@property (nonatomic, strong) NSArray *capital_list;

@end

static NSString *HNSH_PurseCellID = @"HNSH_PurseCellID";

@implementation HNSH_PurseViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"我的钱包" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    
    [self configTableView];
    
    [self loadBillStatistics];
    
    self.view.backgroundColor = [UIColor appBGColor];
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+152)];
    imgV.image = [UIImage imageNamed:@"purse_top_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
}

- (void)configTableView {
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.tableView.rowHeight = 60;
    [self.tableView registerClass:[HNSH_PurseCell class] forCellReuseIdentifier:HNSH_PurseCellID];
    self.tableView.tableHeaderView = self.headerView;
}


#pragma mark networking
- (void)refreshData:(NSInteger)page {
    self.page = page;
    [HNSH_UserNetServer requestBillListWithParams:[self getParams] success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.dataArray removeAllObjects];
            }
            NSArray * records = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_IntegralBillModel.class json:records];
            [self.dataArray addObjectsFromArray:array];
            [self endRefresh:array.count>=10];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        [self.tableView reloadData];
        [self.tableView reloadData];
    }];
}

- (void)loadBillStatistics {
    [HNSH_UserNetServer requestBillStatisticsWithType:[NSString stringWithFormat:@"10"] success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.headerView.moneyLabel.text = [NSString stringWithFormat:@"%.2f", [responseObject.resultset[@"main_value"] doubleValue]];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (NSDictionary *)getParams{
    //accountTypes:资金类型  1       积分    2       汇宝       3       汇豆
    //incomeType:收益类型 0:支出 1:收益 null:全部
    //dateRange:时间范围 0:有时间范围 1:无时间范围
    [self getTime];
    NSDictionary * params = @{@"account_type":@(10),
                              @"date_range":@"0",
                              @"year":self.year ? : @"",
                              @"month":self.month ? : @"",
                              @"page_size":@"10",
                              @"page_num":[NSString stringWithFormat:@"%ld", self.page]};
    return params;
}


- (void)getTime{
    if (![self.currentDate isNotBlank]) {
        self.currentDate = [NSString getTodayWithFormatter:@"yyyy-MM"];
    }
    NSArray * array = [self.currentDate componentsSeparatedByString:@"-"];
    if (array.count > 0) {
        self.year = array[0];
    }
    if (array.count > 1) {
        self.month = array[1];
    }
    if (array.count > 2) {
        self.day = array[2];
    }
}

- (void)chooseTime{
//    CGXPickerViewManager *manager = [[CGXPickerViewManager alloc]init];
//    manager.leftBtnBGColor = [UIColor appGreenColor];
//    manager.rightBtnBGColor = [UIColor appGreenColor];
//    manager.titleLabelColor = [UIColor appGreenColor];
//    manager.leftBtnborderColor = [UIColor appGreenColor];
//    manager.rightBtnborderColor = [UIColor appGreenColor];
//    manager.pickerTitleColor = [UIColor appBlackLabelColor];
//    manager.pickerTitleSelectColor = [UIColor appBlackLabelColor];
//    [CGXDatePickerView showDatePickerWithTitle:@"请选择时间" DateType:UIDatePickerModeDate DefaultSelValue:nil MinDateStr:nil MaxDateStr:nil IsAutoSelect:NO ResultBlock:^(NSString *selectValue) {
//        self.currentDate = selectValue;
//        self.headerView.time = self.currentDate;
//        [self.tableView.mj_header beginRefreshing];
//    } Manager:manager];
    
    //年-月
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonth CompleteBlock:^(NSDate *selectDate) {
        
        NSString *dateString = [selectDate stringWithFormat:@"yyyy-MM"];
        NSLog(@"选择的日期：%@",dateString);
//        [btn setTitle:dateString forState:UIControlStateNormal];
        self.currentDate = dateString;
        self.headerView.time = self.currentDate;
        [self.tableView.mj_header beginRefreshing];
    }];
    datepicker.dateLabelColor = [UIColor appBlackLabelColor];//年-月-日-时-分 颜色
    datepicker.datePickerColor = [UIColor appBlackLabelColor];;//滚轮日期颜色
    datepicker.doneButtonColor = [UIColor appGreenColor];;//确定按钮的颜色
    [datepicker show];
}

- (HNSH_PurseTopView *)headerView{
    if (!_headerView) {
        _headerView = [[HNSH_PurseTopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 275+kNavBarAndStatusBarHeight)];
        
        WeakSelf(weakSelf);
        _headerView.chooseTimeBlock = ^{
            [weakSelf chooseTime];
        };
    }
    return _headerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}

//MARK: UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_PurseCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_PurseCellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
